package leetcode_day._2021._202106._1120;

/**
 * @author yzh
 * @version 1.0
 * @date 2021/6/15 22:42
 * 山脉数组的峰顶索引
 * 算法：二分查找
 * 如果一个元素比它相邻的两个数都要大，那么就是结果
 * 如果比它的左边要大，右边要小，那么往右移
 * 反之，往左移
 */
public class _15_852 {

    public int peakIndexInMountainArray(int[] arr) {
        int start = 1, end = arr.length - 2;
        while (start <= end) {
            int mid = start + (end - start) / 2;
            if (arr[mid] > arr[mid - 1] && arr[mid] > arr[mid + 1]) return mid;
            else if (arr[mid] >= arr[mid - 1] && arr[mid] <= arr[mid + 1]) start = mid + 1;
            else end = mid - 1;
        }
        return start;
    }

}
